package io.jee.medusa.firewall;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import io.jee.medusa.orm.condition.Operation;

@Service
@Transactional
public class FirewallServiceImpl implements FirewallService {
	
	@Resource
	private FirewallActionCountDao firewallDao;

	@Override
	public boolean verifyActionCount(String keyword, long minuteAfter, int count, byte type) {
		long time = System.currentTimeMillis()-minuteAfter*60*1000;
		long size = firewallDao.countCondition().where("keyword", keyword).and("time", Operation.GET, time).and("type", type).end();
		return size<count;
	}
	
	@Override
	public void addActionCount(String keyword, byte type) {
		FirewallActionCount firewall = new FirewallActionCount();
		firewall.setKeyword(keyword);
		firewall.setTime(System.currentTimeMillis());
		firewall.setType(type);
		firewallDao.save(firewall);
	}
	
	@Override
	public void clearActionCount() {
		firewallDao.deleteOverdue(7);
	}

}
